#include "cclientstate.h"

CClientState::CClientState(CClientData* pClient, QWidget *parent)
    : QWidget(parent), m_pClient(pClient)
{
	ui.setupUi(this);
	m_sNormalLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(120, 120, 120);";
	m_sWorkLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(0, 255, 0);";
	m_sAlarmLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255, 0, 0);";
	m_sWarningLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255,255,0);";

    ui.labelCPU->hide();
    ui.labelCPUDesc->hide();
    ui.labelMemory->hide();
    ui.labelMemoryDesc->hide();

	setName();
	m_pTimer = new QTimer();
	connect(m_pTimer, &QTimer::timeout, this, &CClientState::slotDataFlush);
	m_pTimer->start(1000);//默认1秒刷新一次
}

CClientState::~CClientState()
{}

void CClientState::setName()
{
	ui.labelName->setText(m_pClient->m_sName);
}

void CClientState::slotDataFlush()
{
	m_pClient->m_pMutex->lock();
	if (m_pClient->m_bState)
	{
		ui.labelConnectState->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelConnectState->setStyleSheet(m_sAlarmLamp);
	}
	if (m_pClient->m_bAlarm)
	{
		ui.labelAlarmState->setStyleSheet(m_sAlarmLamp);
	}
	else
	{
		ui.labelAlarmState->setStyleSheet(m_sWorkLamp);
	}
	ui.labelCPU->setText(QString::number(m_pClient->m_dCPU, 'f', 2));
	ui.labelMemory->setText(QString::number(m_pClient->m_dMemory, 'f', 2));
	ui.labelPointCount->setText(QString::number(m_pClient->m_nPointCount));
	m_pClient->m_pMutex->unlock();
}
